<template>
  <div class="login-container">
    <div class="from-container">
      <img src="../assets/logo.png" alt="" />
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="ruleForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-lock"
            v-model="ruleForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item class="login-btn">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginApi } from '@/api/index'
export default {
  data () {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度在 6 到 16 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // loginApi(this.ruleForm).then(res => {
          //   console.log(res)
          //   if (res.status === 200) {
          //     // 登录成功,编程式导航
          //     this.$router.push('/welcome')
          //   } else {
          //   }
          // })
          try {
            const res = await loginApi(this.ruleForm)
            console.log(res)
            // 登录成功,编程式导航
            if (res.data.meta.status === 200) {
              localStorage.setItem('token', res.data.data.token)
              this.$router.push('/')
            } else {
              this.$message.error('登录失败,请重新登录!')
            }
          } catch (error) {
            // 发送请求失败才会走这里
            this.$message.error('错了哦，这是一条错误消息')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
}
.from-container {
  width: 450px;
  height: 304px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background-color: #efefef;
    border: 8px solid #fff;
    width: 80px;
    height: 80px;
    box-shadow: 0px 0px 10px 0px #ccc;
  }
  .el-form {
    padding: 90px 20px;
  }
}
</style>
